<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;
class CategoryController extends Controller
{
    public function __construct()
    {
        $this -> middleware('login');
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $num = $request -> input('num',10);
        $keywords = $request -> input('keywords','');
        //
        $data = DB::table('category as c1')
                -> select('c1.*', DB::raw("concat(c1.path,',',c1.id) as sortpath"))
                -> leftJoin('category as c2', 'c1.pid', '=', 'c2.id')
                -> where('c1.name','like','%'.$keywords.'%')
                -> orderBy('sortpath')
                -> paginate($num) ;

        // dd($data);

        foreach ($data as $key => $value)
        {
            $num = substr_count($value -> path, ',');
            $str = str_repeat('|----', $num);
            $data[$key] -> name = $str.$value -> name;
        }
        return view('admin.category.index',['data' => $data ,'request' => $request -> all(), 'title' => '分类列表']);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
        $data = DB::table('category as c1')
                -> select('c1.*', DB::raw("concat(c1.path,',',c1.id) as sortpath"))
                -> orderBy('sortpath')
                -> get();

        foreach ($data as $key => $value)
        {
            $num = substr_count($value -> path, ',');
            $str = str_repeat('|----', $num);
            $data[$key] -> name = $str.$value -> name;
        }
        return view('admin.category.add', ['title' => '分类添加', 'data' => $data ]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
        $data = $request -> except('_token');
        // dd($data);
        // 获取父path
        if($data['pid'] == 0)
        {
            $self_path = '0';
        }else
        {
            $parent_path = DB::table('category') -> where('id', $data['pid']) -> first() -> path;
            // dd($parent_path);

            // 拼自己的path
            $self_path = $parent_path.','.$data['pid'];
            // 放到数组中

        }

        $data['path'] = $self_path;
        $time = date('Y-m-d H:i:s');
        $data['created_at'] = $time;
        $data['updated_at'] = $time;

        $res = DB::table('category') -> insert($data);
        if($res)
         {
          return redirect('/admin/category') -> with(['info' => '添加成功']);
         }else
         {
          return back() -> with(['info' => '添加失败']);
         }

    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
        $data = DB::table('category as c1')
                -> select('c1.*', DB::raw("concat(c1.path,',',c1.id) as sortpath"))
                -> orderBy('sortpath')
                -> get();

        $cateData = DB::table('category') -> where('id', $id) -> first();
        foreach ($data as $key => $value)
        {
            $num = substr_count($value -> path, ',');
            $str = str_repeat('|----', $num);
            $data[$key] -> name = $str.$value -> name;
        }
        // dd($cateData);
        return view('admin.category.edit', ['title' => '分类编辑', 'data' => $data, 'cateData' => $cateData]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
        $data = $request -> except('_token', '_method');
        // dd($data);
        // 获取父path
        if($data['pid'] == 0)
        {
            $self_path = '0';
        }else
        {
            $parent_path = DB::table('category') -> where('id', $data['pid']) -> first() -> path;
            // dd($parent_path);

            // 拼自己的path
            $self_path = $parent_path.','.$data['pid'];
            // 放到数组中

        }

        $data['path'] = $self_path;
        $time = date('Y-m-d H:i:s');
        $data['updated_at'] = $time;
        // dd($data);
        $res = DB::table('category') -> where('id', $id) -> update($data);
        if($res)
         {
          return redirect('/admin/category') -> with(['info' => '更新成功']);
         }else
         {
          return back() -> with(['info' => '更新失败']);
         }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $res = DB::table('category') -> where('pid', $id) ->first();
        if($res)
        {
            return back() -> with(['info' => '有子分类, 不能删除']);
        }
        //
        $res = DB::table('category') -> where('id', $id) -> delete();
        if($res)
        {
            return redirect('/admin/category') -> with(['info' => '删除成功']);
        }else
        {
            return back() -> with(['info' => '删除失败']);
        }

    }
}
